# SETWD ===========================
rm(list=ls())


# LIBRARIES ===========================

library(tidyverse)
library(foreign)
library(stargazer)
library(lmtest)
library(multiwayvcov)

# IMPORT DATA ===========================

dfgi <- read.csv("data/sve-08_04_24.csv",stringsAsFactors = F)

# State Capacity ===========================

## TAB A13: Logit - State Capacity ----

temp <- dfgi%>%
  filter(!is.na(gini_disp))

m4 <- glm(erosion.strict ~ gini_disp + prs.ge, 
          data=temp, family=binomial)
crse4 <- coeftest(m4, cluster.vcov(m4, temp$country.name))
crse4

m5 <- glm(erosion.strict ~ gini_disp + log(gdppc) + prs.ge, 
          data=temp, family=binomial)
crse5 <- coeftest(m5, cluster.vcov(m5, temp$country.name))
crse5

m6 <- glm(erosion.strict ~ gini_disp + log(gdppc) + year + prs.ge, 
          data=temp, family=binomial)
crse6 <- coeftest(m6, cluster.vcov(m6, temp$country.name))
crse6

stargazer(crse4,crse5,crse6, title="Logit: State Capacity",digits=3,
          header=F,df=F,omit.stat=c("f","ser","aic","ll"),
          add.lines = list(c("Observations", nobs(crse4), nobs(crse5), nobs(crse6))),
          star.cutoffs=c(0.05,0.01,0.001),
          star.char=c("*","**","***"),
          notes="$^{*} p<0.05$; $^{**} p<0.01$; $^{***} p<0.001$",
          covariate.labels=c("Gini","Logged GDP per capita","Year",
                             "Bureaucratic Quality"),
          notes.append=F,table.placement="h!",
          out="tables/apptab-capacity-10_27_24.tex")

## Descriptive Stats ----

# GDP/State Capacity correlation:
temp <- dfgi%>%
  filter(!is.na(gdppc),!is.na(prs.ge))
cor(temp$gdppc,temp$prs.ge)


# Hanson and Sigman data loss:
dfgi%>%
  filter(erosion.strict==1)%>%
  summarise(lost.eroders=mean(year>2015,na.rm=T))
